package com.hitgo.soccer.handler;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CommandHandler extends HttpServlet
{
	private static final long	serialVersionUID			= 1L;

	private static HashMap<Integer, BaseHandler> handlersMap = null;
	public void init(ServletConfig cfg) throws ServletException
	{
		super.init(cfg);
		
		if(handlersMap == null)
		{
			handlersMap = new HashMap<Integer, BaseHandler>();
			handlersMap.put(Commands.CMD_MATCH, MatchHandler.getInstance());
			handlersMap.put(Commands.CMD_TEAM, TeamHandler.getInstance());
			handlersMap.put(Commands.CMD_PLAYER, PlayerHandler.getInstance());
			handlersMap.put(Commands.CMD_PROPS, PropsHandler.getInstance());
		}
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
	{
		System.out.println("match " + System.currentTimeMillis());
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
	{
		String strCmd = req.getParameter("fbCmd");
		
		int nCmd = Integer.parseInt(strCmd);
		BaseHandler handler = handlersMap.get(nCmd/Commands.CMD_MASK);
		if(handler != null)
			handler.cmdHandler(nCmd, req, res);
	}

}